#include "FileAttributes.h"
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
#else
#include <sys/stat.h>
#endif
FileAttributes::FileAttributes()
{
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
	dwFileAttributes = 0;
#else
	d_type = DT_UNKNOWN;
#endif
}
bool FileAttributes::IsDirectory()
{
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
	return (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
	return d_type == DT_DIR;
#endif
}
bool FileAttributes::IsReadOnly() {
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
	return (dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0;
#else
	return false;
#endif
}
bool FileAttributes::IsHidden() {
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
	return (dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0;
#else
	return false;
#endif
}
bool FileAttributes::IsSystem() {
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
	return (dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0;
#else
	return false;
#endif
}

bool FileAttributes::GetAttributes(const gstring& file)
{
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
	dwFileAttributes = GetFileAttributesW(file.c_str());
	return dwFileAttributes != INVALID_FILE_ATTRIBUTES;
#else
	struct stat buf; // https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
	if (stat(file.c_str(), &buf) == -1) { // stat: https://pubs.opengroup.org/onlinepubs/009696799/functions/stat.html
		d_type = DT_UNKNOWN;
	}
	else
	{
		if (S_ISDIR(buf.st_mode))
		{
			d_type = DT_DIR;
		}
		else
		{
			d_type = DT_REG;
		}
	}
	return d_type != DT_UNKNOWN;
#endif
}